import json

from django.db import models, transaction
from django.db.models import F

import settings
from social.account.models import User
from social.notification.models import Notification
from social.utils.models import BaseModel
from social.utils.shortcut import get_object_or_none


class Post(BaseModel):
    # 内容
    content = models.TextField(null=True)
    # 浏览数量
    see_count = models.IntegerField(default=0)
    # 点赞数量
    like_count = models.IntegerField(default=0)
    # 评论数量
    comment_count = models.IntegerField(default=0)
    # 相册
    photos = models.TextField(default='')
    # 用户id
    user = models.ForeignKey(User, related_name='posts', on_delete=models.CASCADE)
    # 经纬度
    lat = models.FloatField(default=0)
    lng = models.FloatField(default=0)
    # 置顶时间
    is_active = models.BooleanField(default=True, db_index=True)

    def delete(self, using=None, keep_parents=False):
        self.is_active = False
        self.save(update_fields=['is_active'])

    def like(self, user):
        with transaction.atomic():
            post_like, created = PostLike.objects.get_or_create(post=self, user=user)
            if created:
                self.like_count = PostLike.objects.filter(post=self).count()
                self.save(update_fields=['like_count'])

                post_user = self.user
                post_user.be_like_count = F('be_like_count') + 1
                post_user.save(update_fields=['be_like_count'])

                photos = list(json.loads(str(self.photos)))
                if user.id != self.user_id:
                    Notification.objects.create(content='赞了你的动态', type=Notification.TYPE_LIKE,
                                                user_id=self.user_id,
                                                from_user=user,
                                                link=f'{settings.APP_NAME}://post/{self.hid}',
                                                thumb=photos[0].get('url'))

    def dis_like(self, user):
        with transaction.atomic():
            post_like = get_object_or_none(PostLike, post=self, user=user)
            if post_like:
                post_like.delete()
                self.like_count = PostLike.objects.filter(post=self).count()
                self.save(update_fields=['like_count'])


class Comment(BaseModel):
    content = models.TextField()
    post = models.ForeignKey(Post, related_name='comments', on_delete=models.CASCADE)
    parent = models.ForeignKey('Comment', related_name='children', on_delete=models.CASCADE, null=True)
    user = models.ForeignKey(User, related_name='comments', on_delete=models.CASCADE)
    reply_user = models.ForeignKey(User, related_name='replies', on_delete=models.CASCADE, null=True)
    child = models.ForeignKey('Comment', related_name='child_comment', on_delete=models.CASCADE, null=True)
    is_active = models.BooleanField(default=True)

    class Meta:
        index_together = [
            ('post', 'is_active', 'created_at')
        ]


class PostLike(BaseModel):
    post = models.ForeignKey(Post, related_name='post_likes', on_delete=models.CASCADE)
    user = models.ForeignKey(User, related_name='post_likes', on_delete=models.CASCADE)

    class Meta:
        unique_together = [
            'post', 'user'
        ]
        index_together = [
            ('post', 'created_at'),
            ('user', 'created_at')
        ]
